/*
*
* Copyright (C) 2004 - 2008 BitLaboratory (see http://www.bitlaboratory.com)
* All rights reserved. 
*
* Redistribution and use in source and binary forms, with or without modification, 
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
*    this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
*    this list of conditions and the following disclaimer in the documentation
*    and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
*    derived from this software without specific prior written permission. 
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
* OF SUCH DAMAGE.
*
* You can find the latest version of this library at http://www.bitlaboratory.com
*
*/


using System;
using System.Text;
using System.Runtime.InteropServices;


namespace BitLaboratory.Win32Api.WindowsInstaller
{
	public delegate int INSTALLUIHANDLER(IntPtr context, int messageType, string message);

	public class APIWindowsInstaller
	{
		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiCloseHandle(IntPtr handle);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiApplyMultiplePatches(string patchPackages, string prodCode, string propertyList);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiCollectUserInfo(string Product);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiConfigureProduct(string Product, int iInstallLevel, int eInstallState);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern IntPtr MsiCreateRecord(int Params);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiCreateTransformSummaryInfo(IntPtr database, IntPtr databaseRef, string xfrmfile, int errcond, int validation);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiDatabaseApplyTransform(IntPtr database, string xfrmfile, int conditions);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiDatabaseCommit(IntPtr msihandle);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiDatabaseExport(IntPtr msihandle, string TableName, string FolderPath, string FileName);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiDatabaseGenerateTransform(IntPtr database, IntPtr databaseRef, string xfrmfile, int resv1, int resv2);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiDatabaseGetPrimaryKeys(IntPtr msihandle, string TableName, ref IntPtr recordhandle);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiDatabaseImport(IntPtr msihandle, string FolderPath, string FileName);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiDatabaseMerge(IntPtr msihandle, IntPtr msihandle2, string TableName);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiDatabaseOpenView(IntPtr msihandle, string query, ref IntPtr viewhandle);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiDetermineApplicablePatches(string prodPackagePath, int cPatchInfo, ref MSIPATCHSEQUENCE[] patchInfo);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiDeterminePatchSequence(string productCode, string userSid, int context, int cPatchInfo, ref MSIPATCHSEQUENCE[] patches);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiDoAction(IntPtr hInst, string actioun);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiEnableLog(int LogMode, string LogFile, int LogAttributes);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiEnableUIPreview(IntPtr msihandle, ref IntPtr preview_handle);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiEnumClients(string szComponent, int iProductIndex, StringBuilder ProductBuffer);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiEnumComponentQualifiers(string szComponent, int iIndex, StringBuilder QualifierBuffer, ref int QualifierBufferLength, StringBuilder ApplicationDataBuffer, ref int ApplicationDataBufferLength);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiEnumFeatures(string szProduct, int iFeatureIndex, StringBuilder FeatureBuffer, StringBuilder ParentBuffer);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiEnumPatchesEx(string prodCode, string userSid, int context, int filter, int index, ref string patchCode, ref string targetProdCode, ref int targetContext, ref string targetUserSid, ref int pcchTargetUserSid);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiEnumProducts(int iProductIndex, StringBuilder ProductBuffer);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiEnumProductsEx(string productCode, string userSid, int context, int index, ref string installedProdCode, ref int installContext, ref string sid, ref int cchSid);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiEnumRelatedProducts(string UpgradeCode, int dwReserved, int iProductIndex, StringBuilder ProductBuffer);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiExtractPatchXMLData(string patchPath, int reserved, ref string xmlData, ref int cchXMLdata);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiFormatRecord(IntPtr hInstall, IntPtr hRecord, string szResultBuf, ref int pcchResultBuf);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern IntPtr MsiGetActiveDatabase(IntPtr prod);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiGetDatabaseState(IntPtr msihandle);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiGetFileHash(string path, int options, ref MSIFILEHASHINFO info);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiGetFileSignatureInformation(string szSignedObjectPath, int dwFlags, ref IntPtr ppcCertContext, IntPtr pbHashData, ref int pcbHashData);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiGetPatchInfoEx(string patchCode, string productCode, string userId, int context, string property, ref string val, ref int cchVal);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiGetProductCode(string Component, StringBuilder Buffer39);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiGetProductInfo(string Product, string Attribute, StringBuilder Buffer, ref int length);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiGetProductInfoEx(string productCode, string userSid, int context, string property, ref string value, ref int cchValue);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiGetSourcePath(IntPtr hInstall, string folder, StringBuilder outBuf, ref int outBufLength);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiGetSummaryInformation(IntPtr msihandle, string szDatabasePath, int uiUpdateCount, ref IntPtr SummaryInfo);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiGetUserInfo(string Product, StringBuilder UserNameBuffer, ref int UserNameBufferLength, StringBuilder OrgNameBuffer, ref int OrgNameBufferLength, StringBuilder SerialBuffer, ref int SerialBufferLength);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiInstallProduct(string PackagePath, string CommandLine);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiOpenDatabase(string dbpath, int persist, ref IntPtr msihandle);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiOpenPackage(string path, ref IntPtr product);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiOpenPackageEx(string path, int options, ref IntPtr product);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiOpenProduct(string product, ref IntPtr productHandle);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiPreviewBillboard(IntPtr preview_handle, string ControlName, string Billboard);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiPreviewDialog(IntPtr preview_handle, string DialogName);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiProcessMessage(IntPtr hInstall, int eMessageType, IntPtr hRecord);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiQueryComponentState(string productCode, string userSid, int context, string component, ref int dwState);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiQueryFeatureStateEx(string productCode, string userSid, int context, string feature, ref int installState);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiQueryProductState(string Product);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiRecordClearData(IntPtr recordhandle);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiRecordDataSize(IntPtr recordhandle, int Field);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiRecordGetFieldCount(IntPtr recordhandle);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiRecordGetInteger(IntPtr recordhandle, int Field);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiRecordGetString(IntPtr recordhandle, int Field, StringBuilder ValueBuffer, ref int length);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern bool MsiRecordIsNull(IntPtr recordhandle, int Field);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiRecordReadStream(IntPtr recordhandle, int Field, byte[] DataBuffer, ref int len);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiRecordSetInteger(IntPtr recordhandle, int Field, int Value);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiRecordSetStream(IntPtr recordhandle, int Field, string FilePath);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiRecordSetString(IntPtr recordhandle, int Field, string Value);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiReinstallProduct(string Product, int ReinstallMode);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiRemovePatches(string patchList, string productCode, int installType, string propertyList);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiSequence(IntPtr product, string table, int reserved);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern INSTALLUIHANDLER MsiSetExternalUI(INSTALLUIHANDLER proc, int messages, IntPtr context);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiSetInternalUI(int dwUILevel, ref IntPtr winhandle);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiSourceListAddMediaDisk(string prodOrPatchCode, string userSid, int context, int options, int diskId, string volLabel, string diskPrompt);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiSourceListAddSourceEx(string prodOrPatchCode, string userSid, int context, int options, string source, int index);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiSourceListClearAllEx(string prodOrPatchCode, string userSid, int context, int options);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiSourceListClearMediaDisk(string prodOrPatchCode, string userSid, int context, int options, int diskId);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiSourceListClearSource(string prodOrPatchCode, string userSid, int context, int options, string source);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiSourceListEnumMediaDisks(string prodOrPatchCode, string userSid, int context, int options, int index, ref int diskId, ref string volLabel, ref int cchVolLabel, ref string diskPrompt, ref int cchDiskPrompt);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiSourceListEnumSources(string prodOrPatchCode, string userSid, int context, int options, int index, ref string source, ref int cchSource);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiSourceListForceResolutionEx(string prodOrPatchCode, string userSid, int context, int options);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiSourceListGetInfo(string prodOrPatchCode, string userSid, int context, int options, string property, ref string val, ref int cchVal);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiSourceListSetInfo(string prodOrPatchCode, string userSid, int context, int options, string property, string val);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiSummaryInfoGetProperty(IntPtr SummaryInfo, int uiProperty, ref int uiDataType, ref int Value, ref System.Runtime.InteropServices.FILETIME pftValue, StringBuilder ValueBuffer, ref int ValueBufferLength);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiSummaryInfoGetPropertyCount(IntPtr SummaryInfo, ref int PropertyCount);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiSummaryInfoPersist(IntPtr SummaryInfo);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiSummaryInfoSetProperty(IntPtr SummaryInfo, int uiProperty, int uiDataType, int iValue, ref System.Runtime.InteropServices.FILETIME pftValue, string Value);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiViewClose(IntPtr viewhandle);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiViewExecute(IntPtr viewhandle, IntPtr recordhandle);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiViewFetch(IntPtr viewhandle, ref IntPtr recordhandle);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiViewGetColumnInfo(IntPtr viewhandle, int eColumnInfo, ref IntPtr recordhandle);

		[DllImport("msi.dll", CharSet=CharSet.Auto)]
		public static extern int MsiViewModify(IntPtr viewhandle, int eModifyMode, IntPtr recordhandle);
	}
}